/* spearl/include/spearl/spearl.h 
 * 
 * This file is part of spearl. 
 * 
 * Spearl is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * Spearl is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with spearl. If not, see <https://www.gnu.org/licenses/>
 */ 




#ifndef __SPEARL_SYS_H
#define __SPEARL_SYS_H

/* this is for posix, and to port to other systems, just modify it */
#ifdef _POSIX_VERSION
#error "POSIX required. If you want to port spearl to other systems, define"
#error "a few macros in include/spearl/sys.h and recompile spearl. "
#endif

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stddef.h>

/* it's meaningless, just a big negative number */
#define SPL_ERRNO -1024
#define check_call(call) \
	if (unlikely((call) < 0)) { \
		perror(#call); \
		abort(); \
	}

#define check_alloc(call) ({ \
	void *__ret = call; \
	if (unlikely(!__ret)) { \
		perror(#call); \
		abort(); \
        } __ret; })

#define spl_alloc(type) 	check_alloc(malloc(sizeof(type))) __locally
#define spl_free(type, p) 	free(p) __locally
#define spl_make_alloc(type, name) __locally \
	static inline type *__locally spl_##name##_alloc(void) { \
		return (type *) spl_alloc(type); \
	} \
	static inline void __locally spl_##name##_free(type *p) { \
		free(p); \
	}

#define struct_of_node(ptr, type) container_of(ptr, type, node)

#endif

